#include <GL/glew.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/freeglut.h>
#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include <cmath>
#include <cstdlib>
#include <string.h>
#include <CInitGL.h>
#include "math_3d.h"
#include "CShaders.h"


CInitGL *CInitGL::instance = 0;
GLuint VBO;


void CInitGL::setInstance(CInitGL *framework) {
	instance = framework;
}








void CInitGL::StartInitGL(int argc, char** argv)
    {
        glutInit(&argc,argv);
        glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
      glutInitWindowSize (1024, 786);
      glutInitWindowPosition(100,100);
      glutCreateWindow("Sector7_0.5");

      InitGlutCallbacks();

      GLenum res = glewInit();
    if (res != GLEW_OK) {
      fprintf(stderr, "Error: '%s'\n", glewGetErrorString(res));
      //return 1;
    }

        glClearColor(0.0f,0.0f,0.0f,0.0f);

        CreateVertexBuffer();

        CompileShaders();

        glutMainLoop();

    }

void CInitGL::RenderScene()
    {
       glClear(GL_COLOR_BUFFER_BIT);

    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

    glDrawArrays(GL_TRIANGLES, 0, 3);

    glDisableVertexAttribArray(0);

    glutSwapBuffers();
    }



void CInitGL::InitGlutCallbacks()
    {
        glutDisplayFunc(RenderScene);
    }

void CInitGL::CreateVertexBuffer()
    {
        Vector3f Verticies[3];
        Verticies[0] = Vector3f(0.0f,-1.0f,0.0f);
        Verticies[1] = Vector3f(1.0f, -1.0f, 0.0f);
        Verticies[2] = Vector3f(0.0f, 1.0f, 0.0f);

        glGenBuffers(1, &VBO);
        glBindBuffer(GL_ARRAY_BUFFER, VBO);
        glBufferData(GL_ARRAY_BUFFER, sizeof(Verticies), Verticies, GL_STATIC_DRAW);

    }
